package system.student.login.impl;

import system.student.entity.updatepassword.UpdatePasswordRequest;
import system.student.entity.updatepassword.UpdatePasswordState;
import system.student.entity.user.Teacher;
import system.student.entity.user.UserInfo;
import system.student.login.UpdatePasswordStrategy;
import system.student.mapper.TeacherMapper;
import system.student.mapper.UserMapper;
import system.student.util.MapperUtil;

import java.util.Scanner;

public class UsePasswordUpdate implements UpdatePasswordStrategy {
    private static final UserMapper userMapper = MapperUtil.getMapper(UserMapper.class);
    @Override
    public void updatePassword(UpdatePasswordRequest request) {
        String oldPassword = request.getOldPassword();
        UserInfo user = userMapper.selectByUserId(request.getUserId());
        if (!user.getPassword().equals(oldPassword)) {
            System.out.println("密码错误");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入新密码");
        String password = scanner.nextLine();
        userMapper.updatePasswordByUserId(request.getUserId(), password);
        System.out.println("修改成功");
    }

    @Override
    public UpdatePasswordState getState() {
        return UpdatePasswordState.Password;
    }
}
